#!/bin/bash
echo -e "------------------------------------------------------------------------------------------"
echo "此脚本用来解析百度识别case  @auther 李鹏程"
echo "此脚本用来解析百度识别case  @auther 李鹏程"
echo "此脚本用来解析百度识别case  @auther 李鹏程"
echo -e "------------------------------------------------------------------------------------------"

function getJsonValuesByAwk() {
    eval $(awk -v json="$1" -v key="$2" -v defaultValue="$3" 'BEGIN{
        foundKeyCount = 0
        while (length(json) > 0) {
            # pos = index(json, "\""key"\""); ## 这行更快一些，但是如果有value是字符串，且刚好与要查找的key相同，会被误认为是key而导致值获取错误
            pos = match(json, "\""key"\"[ \\t]*?:[ \\t]*");
            if (pos == 0) {if (foundKeyCount == 0) {print key"="defaultValue;} break}

            ++foundKeyCount;
            start = 0; stop = 0; layer = 0;
            for (i = pos + length(key) + 1; i <= length(json); ++i) {
                lastChar = substr(json, i - 1, 1)
                currChar = substr(json, i, 1)

                if (start <= 0) {
                    if (lastChar == ":") {
                        start = currChar == " " ? i + 1: i;
                        if (currChar == "{" || currChar == "[") {
                            layer = 1;
                        }
                    }
                } else {
                    if (currChar == "{" || currChar == "[") {
                        ++layer;
                    }
                    if (currChar == "}" || currChar == "]") {
                        --layer;
                    }
                    if ((currChar == "," || currChar == "}" || currChar == "]") && layer <= 0) {
                        stop = currChar == "," ? i : i + 1 + layer;
                        break;
                    }
                }
            }

            if (start <= 0 || stop <= 0 || start > length(json) || stop > length(json) || start >= stop) {
                if (foundKeyCount == 0) {print key"="defaultValue;} break
            } else {
                temp = substr(json, start, stop - start);
                if(key == "engineType"){
                  if (temp == 0) {
                    temp = "online";
                  } else {
                    temp = "offline";
                  }
                }
                printf("jsonValue=%s;",temp);
            }

            json = substr(json, stop + 1, length(json) - stop)
        }
    }')

}

originFilepath=$1
if [[ -z $originFilepath ]];then
  read -p "填入待筛选log完整路径:" originFilepath;
  if [[ -z $originFilepath ]];then
    exit
  fi
fi

if [ -f $originFilepath ];then
  echo "${originFilepath} is file"
else
  echo "${originFilepath} is not file; And exit"
  exit
fi

# log中需要筛选的TAG，&&
arrayLogItem=("SpeechCallback" "partial" "final_result")
# json中需要解析的字段
arrayJsonItem=("sn" "engineType" "corpus_no" "best_result")

logGrepItem=""
for(( i=0;i<${#arrayLogItem[@]};i++)) do
  	logGrepItem="${logGrepItem} |grep ${arrayLogItem[i]}"
done;

echo -e "--------------`date "+%Y-%m-%d %H:%M:%S"`-------------------------------------------------"
echo ${arrayJsonItem[*]} | awk -v FS=" " -v OFS=" " '{printf "%-42s %-15s %-25s %s\n",$1,$2,$3,$4}'
cmd="cat $originFilepath $logGrepItem --line-buffer"
echo $cmd | sh | while read line
do
#  echo -e "---------------------------------------------------------------------------------------"
#  echo -e "$line\n"
  line=${line%\}*}
  line=${line#*\{}
  line={${line}}
  line=${line//" "/""}

  arrayJsonValue=()

  for(( i=0;i<${#arrayJsonItem[@]};i++)) do
    jsonValue="null"
  	getJsonValuesByAwk $line ${arrayJsonItem[i]} ""
  	arrayJsonValue[i]=$jsonValue
  done;

  printf "%-42s %-15s %-25s %s\n" ${arrayJsonValue[0]} ${arrayJsonValue[1]} ${arrayJsonValue[2]} ${arrayJsonValue[3]}
#  echo ${arrayJsonValue[*]} | awk -v FS=" " -v OFS="\t" '{printf "%-42s %-15s %-25d %s\n",$1,$2,$3,$4}'
done